/*
 * Public Credit Information Sharing Platform(PCISP).
 * Copyright (c) 2019-2020 jsxzkingdee and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * Please visit https://www.zhengxinsoft.com if you need additional information or have any questions.
 */

package com.platform.core.security.auth;

import com.platform.comm.jwt.JwtTokenUtil;
import lombok.SneakyThrows;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Objects;
import java.util.Optional;

/**
 * 功能描述: 获取认证信息。
 *
 * @author coder
 */
public class AuthenticationFacade {

    @SneakyThrows
//    private static JwtTokenUtil.JwtUser getAuthentication() {
//        String token = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("Authorization");
//        token = authorization.replace(jwtProperties.getTokenHead(), "");
//        return JSONUtil.toBean(URLDecoder.decode(payload, StandardCharsets.UTF_8.name()),JwtTokenUtil.JwtUser.class);
//    }

    private static JwtTokenUtil.JwtUser getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if (principal instanceof JwtTokenUtil.JwtUser) {
            return (JwtTokenUtil.JwtUser) principal;
        }
        return null;
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 用户信息
     */
    private static Optional<JwtTokenUtil.JwtUser> getUserGrantedAuthority() {
        JwtTokenUtil.JwtUser user = getAuthentication();
        if (Objects.isNull(user)) {
            return Optional.empty();
        }
        return  Optional.of(getAuthentication());
    }

    public static Boolean isAdminRole() {
        Optional<JwtTokenUtil.JwtUser> optional = getUserGrantedAuthority();
        return optional.map(JwtTokenUtil.JwtUser::isAdmin).orElse(null);
    }

    public static Boolean isDomain() {
        Optional<JwtTokenUtil.JwtUser> optional = getUserGrantedAuthority();
        return optional.map(JwtTokenUtil.JwtUser::isDomain).orElse(null);
    }

    /**
     * 获取当前用户ID
     *
     * @return userID
     */
    public static String getUserId() {
        Optional<JwtTokenUtil.JwtUser> optional = getUserGrantedAuthority();
        return optional.map(JwtTokenUtil.JwtUser::getId).orElse(null);
    }

    /**
     * 获取当前用户ID
     *
     * @return userID
     */
    public static JwtTokenUtil.JwtUser getUser() {
        Optional<JwtTokenUtil.JwtUser> optional = getUserGrantedAuthority();
        return optional.orElse(null);
    }

    /**
     * 获取当前用户所属部门ID
     *
     * @return 部门ID
     */
    public static String getDepartId() {
        Optional<JwtTokenUtil.JwtUser> optional = getUserGrantedAuthority();
        return optional.map(JwtTokenUtil.JwtUser::getDepartId).orElse(null);
    }

    /**
     * 获取部门名称
     *
     * @return 部门名称
     */
    public static String getDepartName() {
        Optional<JwtTokenUtil.JwtUser> optional = getUserGrantedAuthority();
        return optional.map(JwtTokenUtil.JwtUser::getDepartName).orElse(null);
    }

    /**
     * 获取用户名
     *
     * @return 用户名称
     */
    public static String getUserName() {
        Optional<JwtTokenUtil.JwtUser> optional = getUserGrantedAuthority();
        return optional.map(JwtTokenUtil.JwtUser::getUsername).orElse(null);
    }

    /**
     * 获取用户真实名称
     *
     * @return 用户真实名称
     */
    public static String getRealName() {
        Optional<JwtTokenUtil.JwtUser> optional = getUserGrantedAuthority();
        return optional.map(JwtTokenUtil.JwtUser::getRealName).orElse(null);
    }

    /**
     * 获取用户缓存Key
     *
     * @return 获取用户缓存标志
     */
    public static String getUserCacheKey() {
        return getUserId();
    }
}
